package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.event.ToSelectPhotoEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * 形象照普通状态
 * 为上传
 * Created by Jinsen on 2015/6/19.
 */
public class StatePhotoNormalView extends RelativeLayout implements PersonalPhotoStateView {

    /**
     * 图的地址
     */
    public String mUrl = null;

    /**
     * 图片的宽高比
     */
    public float mPhotoScale = 1.0f;

    ImageView mPrimevalPicIv = null;
    ImageView mWordedPicIv = null;

    ImageView mPrimevalPicIv_temp = null;
    ImageView mWordedPicIv_temp = null;

    Context mContext = null;

    public StatePhotoNormalView(Context context) {
        this(context, null, 0);
    }

    public StatePhotoNormalView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatePhotoNormalView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.personal_photo_state_normal_layout,this);

        mContext = context;

        setPictureImageViewSize();
    }

    private void setPictureImageViewSize(){
        mPrimevalPicIv = (ImageView) findViewById(R.id.state_normal_primeval_imageView);
        mWordedPicIv = (ImageView) findViewById(R.id.state_normal_worked_imageView);

        mPrimevalPicIv_temp = (ImageView) findViewById(R.id.state_normal_primeval_imageView_temp);
        mPrimevalPicIv_temp.setOnClickListener(mSelectPicListener);
        mWordedPicIv_temp = (ImageView) findViewById(R.id.state_normal_worked_imageView_temp);

        resetPhotoViewSize();
    }

    private OnClickListener mSelectPicListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().post(new ToSelectPhotoEvent());
        }
    };

    @Override
    public void setPhotoUrl(String url) {
        mUrl = url;
    }

    @Override
    public void setPhotoScale(float scale) {
        mPhotoScale = scale;

        resetPhotoViewSize();
    }

    @Override
    public void resetPhotoViewSize() {
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        int dp10 = mContext.getResources().getDimensionPixelSize(R.dimen.dp_10);

        int picWidth = (width - dp10 - dp10 - dp10)/2;
        mPrimevalPicIv.getLayoutParams().width = picWidth;
        mPrimevalPicIv.getLayoutParams().height = (int) (mPhotoScale*picWidth);

        mWordedPicIv.getLayoutParams().width = picWidth;
        mWordedPicIv.getLayoutParams().height = (int) (mPhotoScale*picWidth);

        mPrimevalPicIv_temp.getLayoutParams().width = picWidth;
        mPrimevalPicIv_temp.getLayoutParams().height = (int) (mPhotoScale*picWidth);

        mWordedPicIv_temp.getLayoutParams().width = picWidth;
        mWordedPicIv_temp.getLayoutParams().height = (int) (mPhotoScale*picWidth);
    }

    @Override
    public void setState(int state) {
    }

    public void setPhotoBitmap(Bitmap photo){
        if(photo != null) {
            mPrimevalPicIv.setImageBitmap(photo);
            mWordedPicIv.setImageBitmap(photo);
        }else{
            mPrimevalPicIv.setImageDrawable(new ColorDrawable(Color.parseColor("#44000000")));
        }
    }

}
